package com.by_man.presenter.module_health_records.access_records;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.by_man.presenter.R;
import com.by_man.presenter.module_gestureLock.GestureLockActivity;
import com.by_man.repository.UserInfos;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by man on 2017-03-16.
 */
public class HospitalRecordActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_record);
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, AccessRecordsActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(this, AccessRecordsActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        return super.onKeyDown(keyCode,event);
    }

    protected void onRestart()
    {
        super.onRestart();
        Intent intent=new Intent(this,GestureLockActivity.class);
        intent.putExtra("class",this.getClass().getName());
        intent.putExtra("position", 0);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    private void initView() {
        // 初始化ToolBar
        Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        final ListView listView=(ListView)findViewById(R.id.medical_history_item);
        final ArrayList<Map<String, Object>> listItem = new ArrayList<Map<String, Object>>();
        final ArrayList<Map<String, Object>> listItem_hospital_path = new ArrayList<Map<String, Object>>();
        String records = UserInfos.Hospital_Records;
        if (null != records) {
            try {
                JSONArray jsonArray = new JSONArray(records);
                JSONObject temp;
                if (jsonArray.length() == 0) {
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("showdate", "用户暂无数据上传");
                    listItem.add(map);
                    SimpleAdapter simpleAdapter;
                    simpleAdapter = new SimpleAdapter(HospitalRecordActivity.this, listItem, R.layout.item_listview_examine,
                            new String[]{"showdate"},
                            new int[]{R.id.examination_report_date});
                    listView.setAdapter(simpleAdapter);

                } else {
                    for (int j = 0; j < jsonArray.length(); j++) {
                        temp = (JSONObject) jsonArray.get(j);
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        String date = temp.getString("updated_at");
                        if (j > 0) {
                            JSONObject temp2 = (JSONObject) jsonArray.get(j - 1);
                            String date2 = temp2.getString("updated_at");
                            if (!date.equals(date2)) {
                                map.put("showdate", date);
                                listItem.add(map);
                            }
                        } else {
                            map.put("showdate", date);
                            listItem.add(map);
                        }
                    }
                    SimpleAdapter simpleAdapter;
                    simpleAdapter = new SimpleAdapter(HospitalRecordActivity.this, listItem, R.layout.item_listview_examine,
                            new String[]{"showdate"},
                            new int[]{R.id.examination_report_date});
                    listView.setAdapter(simpleAdapter);
                }
            } catch (JSONException je) {
                je.printStackTrace();
            }
        } else {
            Toast.makeText(this,"出现了未知异常，没有相应数据!",Toast.LENGTH_SHORT).show();
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String, String> item = (HashMap<String, String>) listView.getItemAtPosition(position);
                String datetime = item.get("showdate");
                try {
                    listItem_hospital_path.clear();
                    JSONArray jsonArray = new JSONArray(UserInfos.Hospital_Records);
                    JSONObject temp;
                    for (int j = 0; j < jsonArray.length(); j++) {
                        temp = (JSONObject) jsonArray.get(j);
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        String date = temp.getString("updated_at");
                        if(date.equals(datetime))
                        {
                            String path=temp.getString("path");
                            map.put("path", path);
                            listItem_hospital_path.add(map);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent();
                intent.setClass(HospitalRecordActivity.this, HospitalRecordListActivity.class);
                Images.imageHospital_path = listItem_hospital_path;
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
    }
}
